package com.glink.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.glink.common.exception.GeneralSimpleException;
import com.glink.common.util.LogUtil;
import com.glink.common.vo.ResponseResult;
import com.glink.manage.dto.temporaryadmission.*;
import com.glink.manage.service.BarrierGateService;
import com.glink.manage.service.TemporaryAdmissionService;
import com.glink.manage.vo.constructionwork.ConstructionWorkListVO;
import com.glink.manage.vo.temporaryadmission.TemporaryAdmissionCarBaseVO;
import com.glink.manage.vo.temporaryadmission.TemporaryAdmissionCheckWorkVO;
import com.glink.manage.vo.temporaryadmission.TemporaryAdmissionPageVO;
import com.glink.manage.vo.temporaryadmission.TemporaryAdmissionPersonBaseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * @author  qiushaoshan
 * @since  2025-03-04
 */
@RestController
@RequestMapping("/API/Manage/temporaryAdmission")
@Api(tags = "临时准入管理")
@Slf4j
public class TemporaryAdmissionController extends AbstractBaseController {

    @Autowired
    private TemporaryAdmissionService temporaryAdmissionService;
    
    @Autowired
    private BarrierGateService barrierGateService;

    @ApiOperation(value = "01.新增人员临时准入信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/insertPerson", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insertPerson(@Validated @RequestBody TemporaryAdmissionPersonSaveDTO dto) throws GeneralSimpleException {
        log.info("01.新增人员临时准入信息：{}", LogUtil.encode(dto));
        String personId = temporaryAdmissionService.insertPerson(dto);
        boolean status = true;
        if(StringUtils.isNotBlank(personId)){
            CompletableFuture.runAsync(() -> barrierGateService.issuingTemporaryPersonByPersonObjId(personId));
        }else{
            status = false;
        }
        log.info("01.新增人员临时准入信息：success");
        return this.response(status);
    }

    @ApiOperation(value = "02.更新人员准入信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/updatePerson", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updatePerson(@Validated @RequestBody TemporaryAdmissionPersonUpdateDTO dto) throws GeneralSimpleException {
        log.info("02.更新人员准入信息：{}", LogUtil.encode(dto));
        String personId = temporaryAdmissionService.updatePerson(dto);
        boolean status = true;
        if(StringUtils.isNotBlank(personId)){
            CompletableFuture.runAsync(() -> {
                barrierGateService.issuingTemporaryPersonDataDelete(personId);
                barrierGateService.issuingTemporaryPersonByPersonObjId(personId);
            });
        }else{
            status = false;
        }
        log.info("02.更新人员准入信息：success");
        return this.response(status);
    }

    @ApiOperation(value = "03.新增车辆临时准入信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/insertCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> insertCar(@Validated @RequestBody TemporaryAdmissionCarSaveDTO dto) throws GeneralSimpleException {
        log.info("03.新增车辆临时准入信息：{}", LogUtil.encode(dto));
        String carId = temporaryAdmissionService.insertCar(dto);
        boolean status = true;
        if(StringUtils.isNotBlank(carId)){
            CompletableFuture.runAsync(() -> barrierGateService.issuingTemporaryCarToBarrierGateByCarObjId(carId));
        }else{
            status = false;
        }
        log.info("03.新增车辆临时准入信息：success");
        return this.response(status);
    }

    @ApiOperation(value = "04.更新车辆准入信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/updateCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> updateCar(@Validated @RequestBody TemporaryAdmissionCarUpdateDTO dto) throws GeneralSimpleException {
        log.info("04.更新车辆准入信息：{}", LogUtil.encode(dto));
        String carId = temporaryAdmissionService.updateCar(dto);
        boolean status = true;
        if(StringUtils.isNotBlank(carId)){
            CompletableFuture.runAsync(() -> {
                barrierGateService.issuingTemporaryCarDeleteToBarrierGateByCarObjId(carId);
                barrierGateService.issuingTemporaryCarToBarrierGateByCarObjId(carId);
            });
        }else{
            status = false;
        }
        log.info("04.更新车辆准入信息：success");
        return this.response(status);
    }

    @ApiOperation(value = "05.临时准入分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/page", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<IPage<TemporaryAdmissionPageVO>> page(@Validated @RequestBody TemporaryAdmissionPageDTO dto) throws GeneralSimpleException {
        log.info("05.临时准入分页查询：{}", LogUtil.encode(dto));
        IPage<TemporaryAdmissionPageVO> pageVOIPage = temporaryAdmissionService.pageByTemporaryAdmission(dto);
        log.info("05.临时准入分页查询：success");
        return this.response(pageVOIPage);
    }

    @ApiOperation(value = "06.提交审核")
    @PostMapping(value = "/submitReview", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> submitReview(@Validated @RequestBody TemporaryAdmissionIdDTO dto) throws GeneralSimpleException {
        log.info("06.提交审核：{}", LogUtil.encode(dto));
        Boolean status = temporaryAdmissionService.submitReview(dto);
        log.info("06.提交审核：success");
        return this.response(status);
    }

    @ApiOperation(value = "07.撤回")
    @PostMapping(value = "/withdraw", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<Boolean> withdraw(@Validated @RequestBody TemporaryAdmissionIdDTO dto) throws GeneralSimpleException {
        log.info("07.撤回：{}", LogUtil.encode(dto));
        Boolean status = temporaryAdmissionService.withdraw(dto);
        log.info("07.撤回：success");
        return this.response(status);
    }

    @ApiOperation(value = "08.查询人员临时准入信息")
    @PostMapping(value = "/findByPerson", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<TemporaryAdmissionPersonBaseVO> findByPerson(@Validated @RequestBody TemporaryAdmissionIdDTO dto) throws GeneralSimpleException {
        log.info("08.查询人员临时准入信息：{}", LogUtil.encode(dto));
        TemporaryAdmissionPersonBaseVO personBaseVO = temporaryAdmissionService.findByPerson(dto);
        log.info("08.查询人员临时准入信息：success");
        return this.response(personBaseVO);
    }

    @ApiOperation(value = "09.查询车辆临时准入信息")
    @PostMapping(value = "/findByCar", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<TemporaryAdmissionCarBaseVO> findByCar(@Validated @RequestBody TemporaryAdmissionIdDTO dto) throws GeneralSimpleException {
        log.info("08.查询人员临时准入信息：{}", LogUtil.encode(dto));
        TemporaryAdmissionCarBaseVO carBaseVO = temporaryAdmissionService.findByCar(dto);
        log.info("08.查询人员临时准入信息：success");
        return this.response(carBaseVO);
    }

    @ApiOperation(value = "10.查询可办理准入的在建工程")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @GetMapping(value = "/findWorkList", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<ConstructionWorkListVO>> getWorkList() throws GeneralSimpleException {
        log.info("10.查询可办理准入的在建工程");
        List<ConstructionWorkListVO> workList = temporaryAdmissionService.getWorkList();
        log.info("10.查询可办理准入的在建工程：success");
        return this.response(workList);
    }

    @ApiOperation(value = "11.校验是否存在其它准入")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "orgId", value = "当前登录用户的orgId", dataType = "string", required = true),
            @ApiImplicitParam(paramType = "header", name = "userId", value = "当前登录用户的userId", dataType = "string", required = true)})
    @PostMapping(value = "/findWorkAccess", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseResult<List<TemporaryAdmissionCheckWorkVO>> findWorkAccess(@Validated @RequestBody TemporaryAdmissionWorkDTO dto) throws GeneralSimpleException {
        log.info("11.校验是否存在其它准入：{}", LogUtil.encode(dto));
        List<TemporaryAdmissionCheckWorkVO> checkWorkVOList = temporaryAdmissionService.findWorkAccess(dto);
        log.info("11.校验是否存在其它准入：success");
        return this.response(checkWorkVOList);
    }
}
